package com.example.shiyan3;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    // 定义一个访问图片的数组
    int[] images = new int[] { R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4, R.drawable.image5 };
    // 定义默认显示的图片
    int currentImg = 0;
    // 定义图片的初始透明度
    private int alpha = 255;
    MediaPlayer mediaPlayer1 = null;

    @SuppressLint({"CutPasteId", "ClickableViewAccessibility"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获得组件
        final Button plus = findViewById(R.id.plus);
        final Button minus = findViewById(R.id.minus);
        final Button next = findViewById(R.id.next);
        final Button back = findViewById(R.id.back);
        final Button Btn1 = findViewById(R.id.a);
        final Button Btn2 = findViewById(R.id.b);
        @SuppressLint(value = "CutPasteId") final ImageView image1;
        image1 = findViewById(R.id.image1);
        final ImageView image2 = findViewById(R.id.image1);
        // 定义切换图片的方法
        Btn1.setOnClickListener((View v) -> {
            Intent intent=new Intent(MainActivity.this, fragmentation.class);
            startActivity(intent);
            mediaPlayer1 = MediaPlayer.create(this, R.raw.kill);
            mediaPlayer1.start();
        });
        Btn2.setOnClickListener((View v) -> {
            Intent intent=new Intent(MainActivity.this,all_main.class);
            startActivity(intent);});
        OnClickListener listener2 = v -> {

            BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
                    .getDrawable();
            // 如果图片还未回收，先强制回收该图片
            if (!bitmapDrawable.getBitmap().isRecycled()) {
                bitmapDrawable.getBitmap().recycle();
            }
            if (v == next) {
                if (currentImg >= 4) {
                    currentImg = -1;
                }

                image1.setImageBitmap(BitmapFactory.decodeResource(
                        getResources(), images[++currentImg]));
            } else if (v == back) {
                if (currentImg <= 0) {
                    currentImg = 5;
                }
                image1.setImageBitmap(BitmapFactory.decodeResource(
                        getResources(), images[--currentImg]));
            }
        };
        // 定义改变图片透明度的方法
        OnClickListener listener = v -> {
            if (v == plus) {
                alpha += 20;
            }
            if (v == minus) {
                alpha -= 20;
            }
            if (alpha >= 255) {
                alpha = 255;
            }
            if (alpha <= 0) {
                alpha = 0;
            }
            // 设置图片的透明度
            image1.setAlpha(alpha);
        };
        // 为四个按钮添加监听器
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);
        next.setOnClickListener(listener2);
        back.setOnClickListener(listener2);
        image1.setOnTouchListener((arg0, event) -> {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) image1
                    .getDrawable();
            // 获取第一个图片显示框中的位图
            Bitmap bitmap = bitmapDrawable.getBitmap();
            // bitmap图片实际大小与第一个ImageView的缩放比例
            double scale = bitmap.getWidth() / 320.0;
            // 获取需要显示的图片的开始点
            int x = (int) (event.getX() * scale);
            int y = (int) (event.getY() * scale);
            if (x + 120 > bitmap.getWidth()) {
                x = bitmap.getWidth() - 120;
            }
            if (y + 120 > bitmap.getHeight()) {
                y = bitmap.getHeight() - 120;

            }
            // 显示图片的指定区域
            image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120,
                    120));
            image2.setAlpha(alpha);
            return false;
        });
    }
}


